package com.example.dsyncpluginspringbootsamples;

import com.aiks.dsyncpluginspringbootautoconfigure.enums.ModuleTypeEnum;
import com.aiks.dsyncpluginspringbootautoconfigure.plugin.factory.PluginFactory;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.ModuleConfigEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.OrgFieldEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.OrgPlugEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.OrgPlugParamsEntity;
import com.aiks.dsyncpluginspringbootautoconfigure.pojo.SaasUserDTO;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * PluginFactoryTest.
 *
 * @author <a href="mailto:yuanz@yxt.com">yuanz</a>
 * @date 2022-07-19 11:25
 * @since 1.0.0
 **/
@RestController
@RequestMapping("/plugin")
public class PluginFactoryController {
    
    @Autowired
    private PluginFactory pluginFactory;


    @PostMapping("/user")
    public List<SaasUserDTO> handleUser(@RequestBody List<Object> clientDatas) {
        List<SaasUserDTO> saasUserDTOS = Lists.newArrayList();
        pluginFactory.getPluginChain(ModuleTypeEnum.user).execute(getModuleConfig(), saasUserDTOS, clientDatas);
        return saasUserDTOS;
    }

    private ModuleConfigEntity getModuleConfig() {
        ModuleConfigEntity userModuleConfigEntity = new ModuleConfigEntity();
        userModuleConfigEntity.setOrgId("dc9de391-e4e9-4704-a881-ba7b74a37e57");
        userModuleConfigEntity.setDatacode("2022-07-6");
        userModuleConfigEntity.setLastdata(false);
        userModuleConfigEntity.setModuleType(ModuleTypeEnum.user.getValue());
        //插件列表
        OrgPlugEntity orgPlugEntity1 = new OrgPlugEntity();
        orgPlugEntity1.setPlugName("UserPorpertiesPlugin");
        OrgPlugEntity orgPlugEntity2 = new OrgPlugEntity();
        orgPlugEntity2.setPlugName("UserWhite4DeptConditionPlugin");
        //插件参数
        OrgPlugParamsEntity orgPlugParamsEntity1 = new OrgPlugParamsEntity();
        orgPlugParamsEntity1.setSaasFieldName("deptThirdId");
        orgPlugParamsEntity1.setHandleValues("111,122");
        orgPlugEntity2.setOrgPlugParamsEntities(Arrays.asList(orgPlugParamsEntity1));
        userModuleConfigEntity.setOrgPlugEntities(Arrays.asList(orgPlugEntity1, orgPlugEntity2));
        //属性映射
        OrgFieldEntity orgFieldEntity1 = new OrgFieldEntity();
        orgFieldEntity1.setFieldName("dd");
        orgFieldEntity1.setSaasFieldName("username");
        OrgFieldEntity orgFieldEntity2 = new OrgFieldEntity();
        orgFieldEntity2.setFieldName("deptId");
        orgFieldEntity2.setSaasFieldName("deptThirdId");
        userModuleConfigEntity.setOrgFieldEntities(Arrays.asList(orgFieldEntity1, orgFieldEntity2));
        return userModuleConfigEntity;
    }

}
